interface Props {
	params?: any;
}

import styles from "./index.module.less";
import { TABLE_COLUMNS } from "./constant";
import SuperTable from "@/components/SuperTable";
import { deleteProduct, getProductList, updateProductStatus } from "@/api/modules/product";
import { omit } from "lodash";
import { useSelector } from "@/store/hooks";
import { SettingType } from "@/enums";
import { PRODUCT_STATUS_VALUES } from "../constant";
import { RESULT_CODES } from "@/api/helper/ResultCode";
import { Modal, message } from "antd";
import { useState } from "react";
import useUrl from "@/hooks/useUrl";

export default (props: Props) => {
	const { navigate } = useUrl();
	const [params2, setParams2] = useState({});
	const { params } = props;
	const goodsStatusLabels = useSelector(state => state.auth.settingMaps?.[SettingType.GOODS_STATUS_LABEL]);

	const handleRequest = async (params: any) => {
		//字段必须要传，没有就传空，不然接口就报错
		const res = await getProductList({
			limit: params.pageSize,
			page: params.current,
			product_status: params.product_status ?? "",
			product_name: params.product_name ?? "",
			category_id: params.category_id ?? "",
			product_id: params.product_id ?? "",
			shop_name: params.shop_name ?? ""
		});
		return {
			total: res?.total ?? 0,
			data: res?.data ?? []
		};
	};

	const newColumns = [
		...TABLE_COLUMNS(goodsStatusLabels),
		{
			title: "操作",
			width: 180,
			fixed: "right",
			dataIndex: "action-render",
			onClick: (key: string, record: any) => {
				switch (key) {
					case "down":
					case "up":
						updateProductStatus({
							product_id: record?.product_id,
							product_status: record.product_status === PRODUCT_STATUS_VALUES.sales ? -1 : 1
						}).then((res: any) => {
							if (res?.code === RESULT_CODES.SUCCESS) {
								message.success(`操作成功`);
								setParams2({
									timeStamp: Date.now()
								});
							}
						});
						break;
					case "edit":
						navigate(`/product/createProduct?id=${record?.product_id}`);
						break;
					case "delete":
						Modal.confirm({
							icon: null,
							title: "确认删除该内容？",
							content: "内容删除后将无法恢复，请慎重考虑",
							closable: true,
							onOk: c => {
								deleteProduct({
									product_id: record.product_id
								}).then((res: any) => {
									if (res?.code === RESULT_CODES.SUCCESS) {
										message.success("删除成功");
										setParams2(e => ({
											...e,
											timeStamp: Date.now()
										}));
										c?.();
									}
								});
							}
						});
						break;
				}
			},
			options: (record: any) => {
				switch (record.product_status) {
					case PRODUCT_STATUS_VALUES.sales:
						return [
							{
								label: "编辑",
								value: "edit"
							},
							{
								label: "下架",
								value: "down"
							}
						];
					case PRODUCT_STATUS_VALUES.sale_pending:
						return [
							{
								label: "编辑",
								value: "edit"
							},
							{
								label: "删除",
								value: "delete"
							},
							{
								label: "上架",
								value: "up"
							}
						];
					case PRODUCT_STATUS_VALUES.off_shelf:
						return [
							{
								label: "上架",
								value: "up"
							}
						];
				}

				return [];
			}
		}
	];

	return <SuperTable params={{ ...params, ...params2 }} columns={newColumns} request={handleRequest} />;
};
